package com.hykj.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hykj.dao.DevelopmentsDao;
import com.hykj.toolsbean.StringHandler;
import com.hykj.valuebean.developments;

/**
 * Servlet implementation class DevelopmentsServlet
 */
public class DevelopmentsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DevelopmentsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletpath = request.getServletPath();
		if ("/developments/getAllDevelopments".equals(servletpath)) {
			getAllDevelopments(request, response);
		} else if ("/admin/developments/insertDevelopment".equals(servletpath)) {
			insertDevelopment(request, response);
		} else if ("/developments/viewDevelopment".equals(servletpath)) {
			viewDevelopment(request, response);
		} else if ("/admin/listDevelopments".equals(servletpath)) {
			listDevelopments(request, response);
		} else if ("/admin/developments/viewDevelopment".equals(servletpath)) {
			adminviewDevelopment(request, response);
		} else if ("/admin/developments/deleteDevelopment".equals(servletpath)) {
			deleteDevelopment(request, response);
		} else if ("/developments/getCategoryDevelopments".equals(servletpath)) {
			getCategoryDevelopments(request, response);
		} else if ("/admin/developments/updateDevelopment".equals(servletpath)) {
			updateDevelopment(request, response);
		} else if ("/admin/developments/getCategoryDevelopments".equals(servletpath)) {
			adminGetCategoryDevelopments(request, response);
		}
	}
	
	protected void adminGetCategoryDevelopments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			DevelopmentsDao developmentsDaoIns = new DevelopmentsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/developments/getCategoryDevelopments?category=" + category;
			List NewsList = developmentsDaoIns.getCategoryDevelopments(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", developmentsDaoIns.getDaoPage());
			developmentsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/admin/developments/listDevelopments.jsp");
		rd.forward(request, response);
	}

	protected void getAllDevelopments(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			DevelopmentsDao developmentsDaoIns = new DevelopmentsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/developments/getAllDevelopments";
			List NewsList = developmentsDaoIns.getAllDevelopments(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", developmentsDaoIns.getDaoPage());
			developmentsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/developments/developments.jsp");
		rd.forward(request, response);
	}
	
	protected void getCategoryDevelopments(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			DevelopmentsDao developmentsDaoIns = new DevelopmentsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/developments/getCategoryDevelopments?category=" + category;
			List NewsList = developmentsDaoIns.getCategoryDevelopments(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", developmentsDaoIns.getDaoPage());
			developmentsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/developments/developments.jsp");
		rd.forward(request, response);
	}
	
	protected void updateDevelopment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DevelopmentsDao developmentsDaoIns = new DevelopmentsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		developments developmentIns = new developments();
		developmentIns.setArticle(article);
		developmentIns.setAuthor(author);
		developmentIns.setDate(dateformat.format(date));
		developmentIns.setOriginal(original);
		developmentIns.setTitle(title);
		developmentIns.setCategory(category);
		developmentIns.setId(StringHandler.strToint(request.getParameter("id")));
		
		developmentsDaoIns.updateDevelopments(developmentIns);
		developmentsDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listDevelopments");
		rd.forward(request, response);
	}

	protected void insertDevelopment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		DevelopmentsDao developmentsDaoIns = new DevelopmentsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		developments developmentIns = new developments();
		developmentIns.setArticle(article);
		developmentIns.setAuthor(author);
		developmentIns.setDate(dateformat.format(date));
		developmentIns.setOriginal(original);
		developmentIns.setTitle(title);
		developmentIns.setCategory(category);

		developmentsDaoIns.insertDevelopments(developmentIns);
		developmentsDaoIns.closed();

		RequestDispatcher rd = request.getRequestDispatcher("/admin/listDevelopments");
		rd.forward(request, response);
	}

	protected void viewDevelopment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			DevelopmentsDao developmentDaoIns = new DevelopmentsDao();
			developments developmentsIns = developmentDaoIns.getSingleDevelopment(newId);
			developmentDaoIns.closed();

			if (developmentsIns != null) {
				request.setAttribute("newsIns", developmentsIns);
			}

		}
		RequestDispatcher rd = request.getRequestDispatcher("/developments/viewDevelopment.jsp");
		rd.forward(request, response);
	}

	protected void listDevelopments(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			DevelopmentsDao developmentsDaoIns = new DevelopmentsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/admin/listDevelopments";
			List NewsList = developmentsDaoIns.getAllDevelopments(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", developmentsDaoIns.getDaoPage());
			developmentsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/developments/listDevelopments.jsp");
		rd.forward(request, response);
	}

	protected void adminviewDevelopment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			DevelopmentsDao newDaoIns = new DevelopmentsDao();
			developments developmentsIns = newDaoIns.getSingleDevelopment(newId);
			newDaoIns.closed();

			if (developmentsIns != null) {
				request.setAttribute("newsIns", developmentsIns);
			}

		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/developments/updateDevelopments.jsp");
		rd.forward(request, response);
	}

	protected void deleteDevelopment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newId = request.getParameter("newId");
		DevelopmentsDao developmentDaoIns = new DevelopmentsDao();
		developmentDaoIns.deleteDevelopment(newId);
		developmentDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listDevelopments");
		rd.forward(request, response);

	}

}
